package com.bub.pay.core.manager.system.impl;

import com.bub.pay.core.base.BaseManagerAbstract;
import com.bub.pay.core.dao.SysBankDao;
import com.bub.pay.core.entity.domain.SysBank;
import com.bub.pay.core.entity.dto.merchant.SysBankDto;
import com.bub.pay.core.entity.vo.merchant.BankVo;
import com.bub.pay.core.manager.system.SysBankManager;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;

@Service
public class SysBankManagerImpl extends BaseManagerAbstract<SysBank, SysBankDao> implements SysBankManager {

    @Override
    public List<SysBankDto> queryBankOptionList(BankVo vo) {
        Example example = new Example(SysBank.class);
        Example.Criteria criteria = example.createCriteria();
        if(StringUtils.isNotBlank(vo.getBankName())){
            criteria.andLike("name",String.format("%s%s%s","%",vo.getBankName(),"%"));
        }
        criteria.andEqualTo("delFlag", false);
        List<SysBank> sysBankList = getByExample(example);
        if (CollectionUtils.isEmpty(sysBankList)) {
            return new ArrayList<>();
        }

        List<SysBankDto> sysBankDtoList = new ArrayList<>();
        for (SysBank sysBank : sysBankList) {
            SysBankDto dto = new SysBankDto();
            BeanUtils.copyProperties(sysBank, dto);
            sysBankDtoList.add(dto);
        }
        return sysBankDtoList;
    }

    @Override
    public SysBank getByCode(String bankCode) {
        Example bankExample = new Example(SysBank.class);
        bankExample.createCriteria().andEqualTo("code",bankCode);
        return getOneByExample(bankExample);
    }
}
